﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using IAM.Interface.Model;
using IAM.Interface.Model.ENUM;
using System.Collections;
using IAM.Interface.Model.Equals;
using System.Web;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 代理设置信息列表
    /// 包含0条或者多条代理设置信息
    /// </summary>
    public class Delegations : AbstractConvertObject, IConvertXml
    {
        #region 构造函数
        public Delegations()
        {
            _delegations = new System.Collections.ArrayList();
        }
        #endregion
        #region 属性
        private System.Collections.ArrayList _delegations;
        private System.Collections.ArrayList _compareresultlist;
        /// <summary>
        /// 代理设置信息的条数
        /// </summary>
        public int Count
        {
            get
            {
                return this.Items.Count;
            }

        }
        /// <summary>
        /// Delegation信息集合
        /// </summary>
        public System.Collections.ArrayList Items
        {
            get
            {
                return _delegations;
            }
            set
            {
                _delegations = value;
            }
        }
        /// <summary>
        /// 比较结果集,包含CompareResult对象
        /// </summary>
        public System.Collections.ArrayList CompareresultList
        {
            get
            {
                return _compareresultlist;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <Delegations");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(">");

                foreach (object obj in this.Items)
                {
                    _xml.Append(((Delegation)obj).XML);
                }
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</Delegations>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                Delegation delegation = new Delegation();
                delegation.CurrentXmlNode = xn;
                delegation = (Delegation)delegation.ConvertToObject();
                this.Items.Add(delegation);
            }
            return this;
        }
        #endregion
        #region 比较并记录对象的不同处
        /// <summary>
        /// 比较并记录对象的不同处
        /// ** CheckedItem的首行最好是唯一标识
        /// 1.将source xml化
        /// 2.将 obj xml化
        /// 3. 实例化CompareresultList
        /// 4. 遍历source的ACISession
        /// 4.1 寻找目标对象主键相同的 ACISession 节点
        /// 4.2 找不到目标项
        /// 4.3 比较 source 和 object 的ACISession 节点是否相同
        /// 5.雷同4
        /// </summary>
        /// <param name="source">源对象</param>
        /// <param name="obj">目标对象</param>
        public ArrayList GetCompareResultList(Delegations source, Delegations obj)
        {
            CompareResult cr = null;
            _compareresultlist = new ArrayList();
            //1.将source xml化
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);

            //2.将 obj xml化
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            //3. 实例化CompareresultList
            _compareresultlist = new ArrayList();
            #region 4. 遍历source的Delegations
            //4. 遍历source的Delegations
            foreach (object oneofsourceitems in source.Items)
            {
                Delegation sourcedelegation = (Delegation)oneofsourceitems;
                string key = "";//主键

                key = sourcedelegation.Module;
                key = IAM.Interface.Common.EnDeCoding.Encode(key);


                //4.1 寻找目标对象主键相同的 ACISession 节点
                XmlNode xnobject = xdobj.SelectSingleNode("//Delegation[@Module='" + key + "']");
                //4.2 找不到目标项
                if (xnobject == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;

                    this.CompareresultList.Add(cr);
                }
                else//找到目标项目
                {
                    //4.3 比较 source 和 object 的ACISession 节点是否相同
                    Delegation objectACIS = new Delegation();
                    objectACIS.CurrentXmlNode = xnobject;
                    objectACIS.ConvertToObject();

                    if (!sourcedelegation.Equals(objectACIS))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;

                        this.CompareresultList.Add(cr);
                    }
                }

            }
            #endregion
            #region 5.遍历object的Delegation
            //5.遍历object的Delegation 
            foreach (object oneofobj in obj.Items)
            {


                Delegation objectACI = (Delegation)oneofobj;
                string key = "";//主键

                key = objectACI.Module;
                key = IAM.Interface.Common.EnDeCoding.Encode(key);


                //5.1 寻找源对象主键相同的 Delegation  节点
                XmlNode xnsource = xdsource.SelectSingleNode("//Delegation[@Module='" + key + "']");
                //5.2 找不到目标项
                if (xnsource == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;

                    this.CompareresultList.Add(cr);
                }
                else//找到源项目
                {
                    //5.3 比较 source 和 object 的Delegation 节点是否相同
                    Delegation sourACIS = new Delegation();
                    sourACIS.CurrentXmlNode = xnsource;
                    sourACIS.ConvertToObject();

                    if (!objectACI.Equals(sourACIS))
                    {
                        foreach (object objresult in this.CompareresultList)
                        {
                            if (((CompareResult)objresult).ID == key)
                            {
                                CompareresultList.Remove(objresult);
                                break;
                            }
                        }
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;

                        this.CompareresultList.Add(cr);
                    }
                }
            }
            #endregion
            if (CompareresultList.Count > 0)
            {
                return CompareresultList;
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
